/* 
 * Copyright (C) 2001-2003 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "FavHubProperties.h"
#include "FavHubDisableRaw.h"

#include "../client/HubManager.h"
#include "../client/ClientProfileManager.h"
//Sulan start svn209
#include "LineDlg.h"
//Sulan start svn28, svn66
//const string FavHubProperties::defaultRawCommands[] = 
//{ 
//	"", 
//	"$To: %[nick] From: %[mynick] $<%[mynick]> You are being kicked because: %[ct] - %[cd]|<%[mynick]> %[mynick] is kicking %[nick] because: %[ct] - %[cd]|$Kick %[nick]|"
//	"<%[mynick]> %[t]kick %[nick] %[clienttype]|",
//	"<%[mynick]> %[t]kick %[nick] %[cheatingdescription]|",
//	"<%[mynick]> %[t]kick %[nick] %[cheatingdescription]|",	
//	"<%[mynick]> %[t]ban %[nick] %[clienttype]|",
//	"<%[mynick]> %[t]ban %[nick] %[cheatingdescription]|",
//	"<%[mynick]> %[t]banip %[ip] %[clienttype]|",
//	"<%[mynick]> %[t]banip %[ip] %[cheatingdescription]|",
//	"$To: %[mynick] From: %[mynick] $<%[mynick]> [ %[nick] ]-> %[ct] - %[cd]|",
//	"$To: %[opchat] From: %[mynick] $<%[mynick]> [ %[nick] ]-> %[ct] - %[cd]|",
//};
//End

LRESULT FavHubProperties::OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
{
	SetDlgItemText(IDC_HUBNAME, entry->getName().c_str());
	SetDlgItemText(IDC_HUBDESCR, entry->getDescription().c_str());
	SetDlgItemText(IDC_HUBADDR, entry->getServer().c_str());
	SetDlgItemText(IDC_HUBNICK, entry->getNick(false).c_str());
	SetDlgItemText(IDC_HUBPASS, entry->getPassword().c_str());
	SetDlgItemText(IDC_HUBUSERDESCR, entry->getUserDescription().c_str());
	// CDM EXTENSION BEGINS FAVS
	//Add to favs
	SetDlgItemText(IDC_OPCHAT, entry->getOpChat().c_str());
	//Sulan start svn117, svn212
	SetDlgItemText(IDC_RAW_ONE, entry->getRawOne().c_str());
	SetDlgItemText(IDC_RAW_TWO, entry->getRawTwo().c_str());
	SetDlgItemText(IDC_RAW_THREE, entry->getRawThree().c_str());
	SetDlgItemText(IDC_RAW_FOUR, entry->getRawFour().c_str());
	SetDlgItemText(IDC_RAW_FIVE, entry->getRawFive().c_str());
	SetDlgItemText(IDC_RAW_SIX, entry->getRawSix().c_str());
	SetDlgItemText(IDC_RAW_SEVEN, entry->getRawSeven().c_str());
	SetDlgItemText(IDC_RAW_EIGHT, entry->getRawEight().c_str());
	SetDlgItemText(IDC_RAW_NINE, entry->getRawNine().c_str());
	SetDlgItemText(IDC_RAW_TEN, entry->getRawTen().c_str());
	//End
	//Sulan start svn209
	int a = IDC_RAW_TEXT1-1;			
	for(int r=1;r<11;++r){
		SetDlgItemText(++a, RAWSETTING(r).c_str());
	}
	//End

	::CheckDlgButton(*this, IDC_USERIP, entry->getUserIp() ? BST_CHECKED : BST_UNCHECKED);
	::CheckDlgButton(*this, IDC_CHECK_CLIENTS, entry->getCheckClients() ? BST_CHECKED : BST_UNCHECKED);
	::CheckDlgButton(*this, IDC_CHECK_FILELISTS, entry->getCheckFilelists() ? BST_CHECKED : BST_UNCHECKED);
	::CheckDlgButton(*this, IDC_CHECK_FAKESHARE, entry->getCheckFakeShare() ? BST_CHECKED : BST_UNCHECKED);
	::CheckDlgButton(*this, IDC_START_CHECKING_ON_CONNECT, entry->getCheckOnConnect() ? BST_CHECKED : BST_UNCHECKED);
	//Sulan start svn?
	::CheckDlgButton(*this, IDC_DISALBE_SEARCH_SELECT, entry->getDisableSearchSelect() ? BST_CHECKED : BST_UNCHECKED);
	//Gabberworld start
	SetDlgItemText(IDC_COUNTRY_CODE_CHECK, entry->getCountryCodeCheck().c_str());//end
	//Sulan start
	SetDlgItemText(IDC_PROTECTED_USERS, entry->getProtectedUsers().c_str());
	::CheckDlgButton(*this, IDC_HIDE_SHARE, entry->getHideShare() ? BST_CHECKED : BST_UNCHECKED);
	//Sulan start 28
	ctrlTrigger.Attach(GetDlgItem(IDC_TRIGGER));
	bool found = false;
	for(int i = 0; i < Util::possibleNumberOfTriggers(); i++) {
		if (entry->getTrigger() == Util::getTriggerTypes(i).c_str())
			found = true;
		ctrlTrigger.AddString(Util::getTriggerTypes(i).c_str());
	}
	if (!found) {
		if (entry->getTrigger() != "") {
			ctrlTrigger.AddString(entry->getTrigger().c_str());
			ctrlTrigger.SetCurSel(ctrlTrigger.FindString(0, entry->getTrigger().c_str()));
		}else{
			ctrlTrigger.SetCurSel(0);
		}
	} else {
		ctrlTrigger.SetCurSel(ctrlTrigger.FindString(0, entry->getTrigger().c_str()));
	}
	//Sulan start svn86
	::CheckDlgButton(*this, IDC_LOG_CHAT, entry->getLogChat() ? BST_CHECKED : BST_UNCHECKED);
	//End
	// pothead starts svn192 svn196 svn207
	SetDlgItemText(IDC_HUB_MESSAGE, entry->getHubMessage().c_str());
	::CheckDlgButton(*this, IDC_USE_MYINFO_DETECT, entry->getUseMyinfoDetect() ? BST_CHECKED : BST_UNCHECKED);

	ctrlRawProfiles.Attach(GetDlgItem(IDC_RAW_PROFILES));

	RawProfile::List lst = ClientProfileManager::getInstance()->getRawProfiles();
	for(RawProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
		RawProfile& cp = *i;	
		ctrlRawProfiles.AddString(cp.getName().c_str());
	}
	// pothead ends
	// CDM EXTENSION ENDS

	CEdit tmp;
	tmp.Attach(GetDlgItem(IDC_HUBNAME));
	tmp.SetFocus();
	tmp.SetSel(0,-1);
	tmp.Detach();
	tmp.Attach(GetDlgItem(IDC_HUBNICK));
	tmp.LimitText(35);
	tmp.Detach();
	tmp.Attach(GetDlgItem(IDC_HUBUSERDESCR));
	tmp.LimitText(35);
	tmp.Detach();
	tmp.Attach(GetDlgItem(IDC_HUBPASS));
	tmp.SetPasswordChar('*');
	tmp.Detach();
	// Flow84 start
	CComboBox tmp2;
	tmp2.Attach(GetDlgItem(IDC_TRIGGER));
	tmp2.LimitText(1);
	tmp2.Detach();
	// end

	CComboBox cRaw;
	cRaw.Attach(GetDlgItem(IDC_COUNTRY_CHECK_RAW));
	int tt = 0;
	for(int n=0;n<11;){
		cRaw.AddString(RAWSETTING(n).c_str());
		if(entry->getCountryCodeRaw() == n){
			tt = n;
		}
		++n;
	}
	cRaw.SetCurSel(tt);
	cRaw.Detach();


	disableRaws = entry->getDisableRaws();
	if (entry->getDisableRaws() == Util::emptyString)
		entry->setDisableRaws("0;0;0;0;0;0;0;0;0;0");
	disableRaws = entry->getDisableRaws();

	CenterWindow(GetParent());
	return FALSE;
}

LRESULT FavHubProperties::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if(wID == IDOK)
	{
		// CDM EXTENSION BEGINS FAVS
		//char buf[256];
		TCHAR buf[512];
		// CDM EXTENSION ENDS
		GetDlgItemText(IDC_HUBNAME, buf, 256);
		entry->setName(buf);
		GetDlgItemText(IDC_HUBDESCR, buf, 256);
		entry->setDescription(buf);
		GetDlgItemText(IDC_HUBADDR, buf, 256);
		entry->setServer(buf);
		GetDlgItemText(IDC_HUBNICK, buf, 256);
		entry->setNick(buf);
		GetDlgItemText(IDC_HUBPASS, buf, 256);
		entry->setPassword(buf);
		GetDlgItemText(IDC_HUBUSERDESCR, buf, 256);
		entry->setUserDescription(buf);
		// CDM EXTENSION BEGINS
		//Add to favs
		GetDlgItemText(IDC_RAW_ONE, buf, 512);
		entry->setRawOne(buf);
		GetDlgItemText(IDC_RAW_TWO, buf, 512);
		entry->setRawTwo(buf);
		GetDlgItemText(IDC_RAW_THREE, buf, 512);
		entry->setRawThree(buf);
		GetDlgItemText(IDC_RAW_FOUR, buf, 512);
		entry->setRawFour(buf);
		GetDlgItemText(IDC_RAW_FIVE, buf, 512);
		entry->setRawFive(buf);
		GetDlgItemText(IDC_RAW_SIX, buf, 512);
		entry->setRawSix(buf);
		GetDlgItemText(IDC_RAW_SEVEN, buf, 512);
		entry->setRawSeven(buf);
		GetDlgItemText(IDC_RAW_EIGHT, buf, 512);
		entry->setRawEight(buf);
		GetDlgItemText(IDC_RAW_NINE, buf, 512);
		entry->setRawNine(buf);
		GetDlgItemText(IDC_RAW_TEN, buf, 512);
		entry->setRawTen(buf);
		GetDlgItemText(IDC_OPCHAT, buf, 512);
		entry->setOpChat(buf);

		//Gabberworld 
		GetDlgItemText(IDC_COUNTRY_CODE_CHECK, buf, 512);
		entry->setCountryCodeCheck(buf);

		CComboBox cRaw(GetDlgItem(IDC_COUNTRY_CHECK_RAW));
		entry->setCountryCodeRaw(cRaw.GetCurSel());
		//End
		
		//Sulan start
		GetDlgItemText(IDC_PROTECTED_USERS, buf, 512);
		entry->setProtectedUsers(buf);
		//Sulan start 28
		GetDlgItemText(IDC_TRIGGER, buf, 2);
		entry->setTrigger(buf);
		//End
		
		CButton btn = ::GetDlgItem(m_hWnd, IDC_USERIP);
		entry->setUserIp(Util::toBool(btn.GetCheck()));
		btn = ::GetDlgItem(m_hWnd, IDC_CHECK_CLIENTS);
		entry->setCheckClients(Util::toBool(btn.GetCheck()));
		btn = ::GetDlgItem(m_hWnd, IDC_CHECK_FILELISTS);
		entry->setCheckFilelists(Util::toBool(btn.GetCheck()));
		btn = ::GetDlgItem(m_hWnd, IDC_CHECK_FAKESHARE);
		entry->setCheckFakeShare(Util::toBool(btn.GetCheck()));
		btn = ::GetDlgItem(m_hWnd, IDC_START_CHECKING_ON_CONNECT);
		entry->setCheckOnConnect(Util::toBool(btn.GetCheck()));
		//Sulan start
		btn = ::GetDlgItem(m_hWnd, IDC_HIDE_SHARE);
		entry->setHideShare(Util::toBool(btn.GetCheck()));
		//Sulan start svn86
		btn = ::GetDlgItem(m_hWnd, IDC_LOG_CHAT);
		entry->setLogChat(Util::toBool(btn.GetCheck()));
		//End
		// pothead starts svn192 svn196 (sulan code)
		GetDlgItemText(IDC_HUB_MESSAGE, buf, 512);
		entry->setHubMessage(buf);
		btn = ::GetDlgItem(m_hWnd, IDC_USE_MYINFO_DETECT);
		entry->setUseMyinfoDetect(Util::toBool(btn.GetCheck()));
		
		// pothead ends
		//Sulan start svn?
		btn = ::GetDlgItem(m_hWnd, IDC_DISALBE_SEARCH_SELECT);
		entry->setDisableSearchSelect(Util::toBool(btn.GetCheck()));
		entry->setDisableRaws(disableRaws);
		// CDM EXTENSION ENDS

		HubManager::getInstance()->save();
	}
	EndDialog(wID);
	return 0;
}

LRESULT FavHubProperties::OnTextChanged(WORD /*wNotifyCode*/, WORD wID, HWND hWndCtl, BOOL& /*bHandled*/) {
	TCHAR buf[256];

	GetDlgItemText(wID, buf, 256);
	tstring old = buf;

	// Strip '$', '|' and ' ' from text
	TCHAR *b = buf, *f = buf, c;
	while( (c = *b++) != 0 ) {
		if(c != '$' && c != '|' && (wID == IDC_HUBUSERDESCR || c != ' '))
			*f++ = c;
	}

	*f = '\0';

	if(old != buf) {
		// Something changed; update window text without changing cursor pos
		CEdit tmp;
		tmp.Attach(hWndCtl);
		int start, end;
		tmp.GetSel(start, end);
		tmp.SetWindowText(buf);
		if(start > 0) start--;
		if(end > 0) end--;
		tmp.SetSel(start, end);
		tmp.Detach();
	}
	return TRUE;
}

// pothead starts svn207
LRESULT FavHubProperties::OnImportCmd(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	RawProfile::List lst = ClientProfileManager::getInstance()->getRawProfiles();
	int count = 0;
	for(RawProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
		RawProfile& cp = *i;
		if(count == ctrlRawProfiles.GetCurSel()) {
			SetDlgItemText(IDC_RAW_ONE, cp.getRaw1().c_str());
			SetDlgItemText(IDC_RAW_TWO, cp.getRaw2().c_str());
			SetDlgItemText(IDC_RAW_THREE, cp.getRaw3().c_str());
			SetDlgItemText(IDC_RAW_FOUR, cp.getRaw4().c_str());
			SetDlgItemText(IDC_RAW_FIVE, cp.getRaw5().c_str());
			SetDlgItemText(IDC_RAW_SIX, cp.getRaw6().c_str());		
			SetDlgItemText(IDC_RAW_SEVEN, cp.getRaw7().c_str());
			SetDlgItemText(IDC_RAW_EIGHT, cp.getRaw8().c_str());
			SetDlgItemText(IDC_RAW_NINE, cp.getRaw9().c_str());
			SetDlgItemText(IDC_RAW_TEN, cp.getRaw10().c_str());
		}
		count++;
	}
	return 0;
}
// pothead ends
//Sulan start svn209
LRESULT FavHubProperties::onSaveRaws(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/){
	TCHAR buf[512];
	StringList sl;
	LineDlg dlg;
	dlg.title = TSTRING(NAME);
	if(dlg.DoModal() == IDOK){
		
		sl.push_back(dlg.line);
		GetDlgItemText(IDC_RAW_ONE, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_TWO, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_THREE, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_FOUR, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_FIVE, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_SIX, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_SEVEN, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_EIGHT, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_NINE, buf, 512);
		sl.push_back(buf);
		GetDlgItemText(IDC_RAW_TEN, buf, 512);
		sl.push_back(buf);
		if(!dlg.line.empty()){
			if(ClientProfileManager::getInstance()->nameInUse(dlg.line)){
				MessageBox(_T("Name is already in use, please try another one"), _T("Error"), MB_OK);
				return 0;
			}
			ClientProfileManager::getInstance()->addRawProfile(sl);
			ctrlRawProfiles.ResetContent();
			RawProfile::List lst = ClientProfileManager::getInstance()->getRawProfiles();
			for(RawProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
				RawProfile& cp = *i;	
				ctrlRawProfiles.AddString(cp.getName().c_str());
			}
		}
	}
	sl.clear();
	return 0;
}

LRESULT FavHubProperties::onDisableRaws(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/){
	FavHubDisableRawDlg dlg;
	dlg.disableRawsDlg = disableRaws;
	if(dlg.DoModal() == IDOK) {
		disableRaws = dlg.disableRawsDlg;
	}
	return 0;
}
//End
/**
 * @file
 * $Id: FavHubProperties.cpp 334 2005-09-03 20:28:33Z Pothead $
 */